package ace.module.upms.core.impl.service.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.module.common.api.constant.CommonConstants;
import ace.module.common.core.impl.properties.ModuleCommonBaseProperties;
import ace.module.common.core.impl.properties.ModuleCommonSecurityProperties;
import ace.module.upms.api.constant.UpmsConstant;
import ace.module.upms.api.enums.UpmsApplicationStatusEnum;
import ace.module.upms.api.enums.UpmsMenuShowStatusEnum;
import ace.module.upms.api.enums.UpmsMenuTypeEnum;
import ace.module.upms.core.impl.dao.entity.UpmsApplication;
import ace.module.upms.core.impl.dao.entity.UpmsMenu;
import ace.module.upms.core.impl.dao.entity.UpmsRole;
import ace.module.upms.core.impl.dao.entity.UpmsRoleAccount;
import ace.module.upms.core.impl.manager.UpmsApplicationManager;
import ace.module.upms.core.impl.manager.UpmsMenuManager;
import ace.module.upms.core.impl.manager.UpmsRoleAccountManager;
import ace.module.upms.core.impl.manager.UpmsRoleManager;
import ace.module.upms.core.impl.service.UpmsInitService;
import java.time.Instant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author caspar
 * @date 2023/8/18 17:42
 */
@Slf4j
@AllArgsConstructor
@Component
public class UpmsInitServiceImpl implements UpmsInitService {

  private final ModuleCommonBaseProperties moduleCommonBaseProperties;
  private final ModuleCommonSecurityProperties moduleCommonSecurityProperties;
  private final UpmsApplicationManager upmsApplicationManager;
  private final UpmsMenuManager upmsMenuManager;
  private final UpmsRoleManager upmsRoleManager;
  private final UpmsRoleAccountManager upmsRoleAccountManager;

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void init() {
    this.initOrNoneAdminUpmsApp();
  }

  private void initOrNoneAdminUpmsApp() {
    if (upmsApplicationManager.getById(moduleCommonSecurityProperties.getUpmsAdminPlatformAppId()) != null) {
      log.info("管理员权限应用已经存在");
      return;
    }
    UpmsApplication upmsApplication = UpmsApplication.builder()
        .id(moduleCommonSecurityProperties.getUpmsAdminPlatformAppId())
        .key(moduleCommonSecurityProperties.getUpmsAdminPlatformAppKey())
        .name("管理员权限应用")
        .remark("管理员权限应用")
        .status(UpmsApplicationStatusEnum.NORMAL.getCode())

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(Instant.now().toEpochMilli())
        .updateTime(Instant.now().toEpochMilli())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();

    UpmsMenu upmsMenuWorkplace = UpmsMenu.builder()
        .id(this.upmsMenuManager.getNewId())
        .upmsAppId(upmsApplication.getId())
        .parentId(UpmsConstant.ROOT_PARENT_ID)
        .icon("")
        .name("工作台")
        .uri("/workplace")
        .sort(CoreConstant.DEFAULT_SORT)
        .showStatus(UpmsMenuShowStatusEnum.DISPLAY.getCode())
        .permission("workplace_read")
        .type(UpmsMenuTypeEnum.MENU.getCode())

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(Instant.now().toEpochMilli())
        .updateTime(Instant.now().toEpochMilli())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();

    UpmsRole upmsRole = UpmsRole.builder()
        .id(CommonConstants.DEFAULT_ADMINISTRATOR_ROLE_ID)
        .upmsAppId(upmsApplication.getId())
        .name(CommonConstants.DEFAULT_ADMINISTRATOR_ROLE_TITLE_AND_REMARK)
        .remark(CommonConstants.DEFAULT_ADMINISTRATOR_ROLE_TITLE_AND_REMARK)
        .key(moduleCommonSecurityProperties.getAdministratorRoleKey())

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(Instant.now().toEpochMilli())
        .updateTime(Instant.now().toEpochMilli())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();

    UpmsRoleAccount upmsRoleAccount = UpmsRoleAccount.builder()
        .id(CommonConstants.DEFAULT_ADMINISTRATOR_ROLE_ID)
        .upmsAppId(upmsApplication.getId())
        .roleId(upmsRole.getId())
        .iamAccountId(moduleCommonSecurityProperties.getIamAdministratorAccountId())

        .deleteFlag(CoreConstant.DELETE_FLAG_NOT)
        .createTime(Instant.now().toEpochMilli())
        .updateTime(Instant.now().toEpochMilli())
        .rowVersion(CoreConstant.DEFAULT_ROW_VERSION)
        .build();

    this.upmsApplicationManager.save(upmsApplication, false);
    this.upmsMenuManager.save(upmsMenuWorkplace, false);
    this.upmsRoleManager.save(upmsRole, false);
    this.upmsRoleAccountManager.save(upmsRoleAccount, false);
    log.info("管理员权限应用初始化成功");
  }
}
